package com.start.pay.core;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.start.common.tools.ClassTools;
import com.start.pay.core.entity.StartPlatformPlayConfig;
import com.start.pay.core.service.StartPlayPlatformConfigService;
import com.start.quartz.model.StartQuartzEntityModel;
import com.start.quartz.service.StartQuartzService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.support.CronExpression;

import java.util.List;

@Slf4j
@Configuration
@MapperScan("com.start.pay.core.mappers.**")
@ComponentScan("com.start.pay.core.**")
public class StartPlatformPayCoreAutoConfig implements CommandLineRunner {


    private static final String quartzId = "startGlobalPayQuartzId";
    @Autowired
    private StartPlayPlatformConfigService playPlatformConfigService;

    @Autowired
    private StartGlobalPayConfig payConfig;

    @Autowired
    private StartQuartzService startQuartzService;

    /**
     * Callback used to run the bean.
     *
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    @Override
    public void run(String... args) throws Exception {
        log.info("开始初始化加载支付平台配置");
        List<StartPlatformPlayConfig> list = playPlatformConfigService.list(new LambdaQueryWrapper<StartPlatformPlayConfig>().ge(StartPlatformPlayConfig::getIsEnable, 1));
        if (list.isEmpty()) return;
        for (StartPlatformPlayConfig config : list) {
            try {
                StartPayPlatformManager.getInstance().addPayPlatformConfig(config.getPlatformType(), config.getAppId(), config);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        log.info("支付平台配置加载完毕：" + list.size());
        StartQuartzEntityModel quartzEntityModel = startQuartzService.getQuartById(quartzId);

        if (!payConfig.isQuartzEnable()) {
            startQuartzService.disableTask(quartzId);
            return;
        }
        if (StringUtils.isAllBlank(payConfig.getQuartzName())) {
            log.error("未配置任务名称：start.pay.quartz-name");
            return;
        }
        if (!ClassTools.hasClass(payConfig.getQuartzClass())) {
            log.error("未配置任务执行类：start.pay.quartz-class");
            return;
        }
        if (StringUtils.isAllBlank(payConfig.getQuartzCron())) {
            log.error("未配置任务时间调度表达式：start.pay.quartz-cron");
            return;
        }
        if (!CronExpression.isValidExpression(payConfig.getQuartzCron())) {
            log.error("配置文件中：start.pay.quartz-cron 表达式错误：" + payConfig.getQuartzCron());
            return;
        }
        if(quartzEntityModel==null){
            quartzEntityModel=new StartQuartzEntityModel();
            quartzEntityModel.setId(quartzId);
            quartzEntityModel.setGroupKey("系统任务");
            quartzEntityModel.setCode(quartzId);
        }
        quartzEntityModel.setName(payConfig.getQuartzName());
        quartzEntityModel.setCron(payConfig.getQuartzCron());
        quartzEntityModel.setClassPath(payConfig.getQuartzClass());
        quartzEntityModel.setIsEnable(1);
        startQuartzService.saveOrUpdate(quartzEntityModel);
        startQuartzService.disableTask(quartzId);
        startQuartzService.activeTask(quartzId);

    }
}
